#include "lightcontrolform.h"
#include "ui_lightcontrolform.h"
#include <QPainter>
#include <QStyleOption>

LightControlForm::LightControlForm(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::LightControlForm)
{
    ui->setupUi(this);
    ui->toolButton_contractExpand->setChecked(false);
    ui->widget_hide->setHidden(true);
    this->setFixedHeight(78);
    ui->radioButton_red->setChecked(true);
    this->setProperty("form","backgroundform");
    ui->label_signalLight->setProperty("model","red");

    // 连接滑块的值变化信号到数字输入框的槽函数
    connect(ui->horizontalSlider, &QSlider::valueChanged, this, &LightControlForm::updateSpinBox);
    // 连接数字输入框的值变化信号到滑块的槽函数
    connect(ui->doubleSpinBox, QOverload<double>::of(&QDoubleSpinBox::valueChanged), this, &LightControlForm::updateSlider);

    // 连接滑块的值变化信号到数字输入框的槽函数
    connect(ui->horizontalSlider_gain, &QSlider::valueChanged, this, &LightControlForm::updateLabelGain);
    connect(ui->horizontalSlider_gamma,&QSlider::valueChanged, this, &LightControlForm::updateLabelGamma);
    connect(ui->horizontalSlider_gainImage,&QSlider::valueChanged, this, &LightControlForm::updateLabelGainImage);
}

LightControlForm::~LightControlForm()
{
    delete ui;
}

void LightControlForm::updateSpinBox(int value)
{
    ui->doubleSpinBox->setValue(value/ 100.0);
    // 发出信号，通知LightSettingsForm更新LED值
    emit sig_updateLedValue(value);
}

void LightControlForm::updateLabelGain(int value)
{
    ui->label_gain->setText(QString::number(value));
    emit sig_updateGain(value);
}

void LightControlForm::updateLabelGamma(int value)
{
    ui->label_gamma->setText(QString::number(value));
    emit sig_updateGamma(value);
}

void LightControlForm::updateLabelGainImage(int value)
{
    ui->label_gainImage->setText(QString::number(value));
    emit sig_updateGainImage(value);
}

void LightControlForm::updateSlider(double value)
{
    ui->horizontalSlider->setValue(static_cast<int>(value*100));
}

void LightControlForm::on_pushButton_restoreDefaultGain_clicked()
{
    ui->horizontalSlider_gain->setValue(5);
}

void LightControlForm::on_pushButton_restoreDefaultGamma_clicked()
{
    ui->horizontalSlider_gamma->setValue(8);
}

void LightControlForm::on_pushButton_restoreDefaultGainImage_clicked()
{
    ui->horizontalSlider_gainImage->setValue(10);
}

void LightControlForm::on_pushButton_restoreDefaults_clicked()
{
    on_pushButton_restoreDefaultGain_clicked();
    on_pushButton_restoreDefaultGamma_clicked();
    on_pushButton_restoreDefaultGainImage_clicked();

    on_radioButton_red_clicked();
}

void LightControlForm::on_toolButton_contractExpand_clicked(bool checked)
{
    ui->widget_hide->setHidden(!checked);
    if(checked)
        this->setFixedHeight(270);
    else
        this->setFixedHeight(78);
}

void LightControlForm::paintEvent(QPaintEvent *event)
{
    QStyleOption opt;
    opt.init(this);
    QPainter painter(this);
    style()->drawPrimitive(QStyle::PE_Widget,&opt,&painter,this);
    QWidget::paintEvent(event);
}

void LightControlForm::on_radioButton_red_clicked()
{
    ui->label_signalLight->setProperty("model","red");
    // 强制样式更新
    ui->label_signalLight->style()->unpolish(ui->label_signalLight);
    ui->label_signalLight->style()->polish(ui->label_signalLight);
}

void LightControlForm::on_radioButton_green_clicked()
{
    ui->label_signalLight->setProperty("model","green");
    // 强制样式更新
    ui->label_signalLight->style()->unpolish(ui->label_signalLight);
    ui->label_signalLight->style()->polish(ui->label_signalLight);
}

void LightControlForm::on_radioButton_blue_clicked()
{
    ui->label_signalLight->setProperty("model","blue");
    // 强制样式更新
    ui->label_signalLight->style()->unpolish(ui->label_signalLight);
    ui->label_signalLight->style()->polish(ui->label_signalLight);
}
